package org.example.interfaces.web;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.example.infrastructure.common.Constants;

import java.io.Serializable;

/**
 * @author liyishan
 * @date 2023/5/8 15:09
 * @apiNote
 */
@ApiModel(value = "Result", description = "返回结果")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回代码", allowableValues = "0,1")
    private int code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result() {
        setCode(Constants.RESULT_TYPE_SUCCESS);
        setMsg(Constants.RESULT_MSG_SUCCESS);
    }

    public Result(T data) {
        setCode(Constants.RESULT_TYPE_SUCCESS);
        setMsg(Constants.RESULT_MSG_SUCCESS);
        setData(data);
    }

    public static <T> Result<T> successResp(T data) {
        Result<T> resp = new Result<T>();
        resp.setCode(Constants.RESULT_TYPE_SUCCESS);
        resp.setMsg(Constants.RESULT_MSG_SUCCESS);
        resp.setData(data);
        return resp;
    }

    public static <T> Result<T> successResp() {
        Result<T> resp = new Result<T>();
        resp.setCode(Constants.RESULT_TYPE_SUCCESS);
        resp.setMsg(Constants.RESULT_MSG_SUCCESS);
        return resp;
    }

    /**
     * 请求失败返回的数据
     *
     * @return ResponseContentOne
     */
    public static <T> Result<T> errorResp() {
        return errorResp(Constants.RESULT_TYPE_FAILURE, Constants.RESULT_MSG_FAILURE);
    }

    /**
     * 请求失败返回的数据
     *
     * @param msg msg
     * @return ResponseContentOne
     */
    public static <T> Result<T> errorResp(String msg) {
        return errorResp(Constants.RESULT_TYPE_FAILURE, msg);
    }

    /**
     * 请求失败返回的数据
     *
     * @param msg   消息提示内容
     * @param <T>
     * @return
     */
    public static <T> Result<T> errorResp(int code, String msg) {
        Result<T> resp = new Result<>();
        resp.setCode(code);
        resp.setMsg(msg);
        return resp;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
